package com.ctu.gzero.elements
{
	import com.ctu.gzero.utils.GzLogger;
	
	import flash.text.engine.ElementFormat;
	import flash.text.engine.FontDescription;
	import flash.text.engine.TextElement;
	
	import mx.logging.*;
	import mx.logging.targets.*;
	
	public class TxtRun extends RunElement
	{
		private var _text:String;
		private var _textElement:TextElement;
		private var  _type:String = "TxtRun";
		private var _elementFormat:ElementFormat;
		private var _fontDescription:FontDescription;
		private var _color:uint;
		private var _fontSize:uint = 12;
		private var _lineHeight:Object = "120%";
				
		public function TxtRun(text:String)
		{
			super.logger = GzLogger.getLogger("TxtRun");
			
			_fontDescription = new FontDescription();
			_fontDescription.fontName = "맑은 고딕";
			_elementFormat = new ElementFormat(_fontDescription);
			_elementFormat.fontSize = _fontSize;
			_text = text;
		}
		
		public function get color():uint
		{
			return _color;
		}
		
		public function set color(val:uint):void
		{
			_color = val;
			_elementFormat.color = _color;
		}
		
		public function get fontSize():uint
		{
			return _fontSize;
		}
		
		public function set fontSize(val:uint):void
		{
			_fontSize = val;
			_elementFormat.fontSize = _fontSize;
		}
		
		public function get elementFormat():ElementFormat
		{
			return _elementFormat;
		}
		
		public function set textElement(val:TextElement):void
		{
			_textElement = val;
		}
		
		public function get textElement():TextElement
		{
			return _textElement;
		}
		
		public function set lineHeight(val:Object):void
		{
			_lineHeight = val;
		}
		
		public function get lineHeight():Object
		{
			return _lineHeight;
		}
		
		public function get text():String
		{
			return _text;	
		}
		
		override public function get type():String
		{
			return this._type;	
		}
		
		public override function get textLength():int
		{
			return _textLength;
		}
		
		public function replaceText(relativeStartPosition:int, relativeEndPosition:int, textValue:String):void
		{
			_text = _text.substring(0, relativeStartPosition).concat(textValue).concat(_text.substring(relativeEndPosition, _text.length));
		}
		
		public function splitByLength(len:int):TxtRun
		{
			var text:String;			
			var txtRun:TxtRun;
			var textLength:int;
			var para:ParaElement;
			
			if(len <= 0)
			{
				return null;
			}
			
			if(len > this._text.length)
			{
				return this;
			}
			
			text = this._text;
			textLength  = this.textLength;
			
			this._text = _text.substr(0, len);
			this.textElement.text = _text.substr(0,len);			
			
			txtRun = new TxtRun(_text.substr(len,textLength-len));
			para = this.para;
			para.getRunIndex(this);
			
			para.runList.addItemAt(txtRun,para.getRunIndex(this));
			para.normalize();			
			return txtRun;			
		}
		
		public override function normalize():void
		{
			super.normalize();
			_textLength = _text.length;
		}
	}
}